package org.jeecgframework.web.material.controller;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.jeecgframework.core.common.controller.BaseController;
import org.jeecgframework.core.common.exception.BusinessException;
import org.jeecgframework.web.system.service.SystemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

/**
 * @Description: finance
 * @date 2015-08-17 10:01:47
 * @version V1.0
 * 
 */
@Scope("prototype")
@Controller
@RequestMapping("/financeController")
public class FinanceController extends BaseController {
	/**
	 * Logger for this class
	 */
	private static final Logger logger = Logger
			.getLogger(FinanceController.class);

	@Autowired
	private SystemService systemService;
	private String message;

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	/**
	 * 页面跳转
	 * 
	 * @return
	 */
	@RequestMapping(params = "finance")
	public ModelAndView finance(HttpServletRequest request) {
		return new ModelAndView("material/finance/financeList");
	}

	/**
	 * 各月份
	 * 
	 * @param request
	 * @param response
	 * @param dataGrid
	 * @param user
	 */

	@RequestMapping(params = "financeMonthSum")
	@ResponseBody
	public HashMap<String, Object> financeMonthSum(HttpServletRequest request,
			HttpServletResponse response) {

		try {

			Calendar cal = Calendar.getInstance();
			int year = cal.get(Calendar.YEAR);
			String incomeSql = "SELECT MONTH(trade_time) month_str,sum(money) moneySum,'1' flag from flow_detail_income where YEAR(trade_time) = '"
					+ year + "' GROUP BY MONTH(trade_time)";
			String payoutSql = "SELECT MONTH(payout_time) month_str,sum(money) moneySum,'2' flag from flow_detail_payout where YEAR(payout_time) = '"
					+ year + "' GROUP BY MONTH(payout_time)";
			List<Map<String, Object>> list_income = systemService
					.findForJdbc(incomeSql);
			List<Map<String, Object>> list_payout = systemService
					.findForJdbc(payoutSql);

			List<Object> monthArray = new ArrayList<Object>();
			List<Object> incomeArray = new ArrayList<Object>();
			List<Object> payoutArray = new ArrayList<Object>();
			for (int i = 1; i <= 12; i++) {
				monthArray.add(i + "月");
				incomeArray.add(getSameMonthSum(i, list_income));
				payoutArray.add(getSameMonthSum(i, list_payout));
			}
			HashMap<String, Object> map = new HashMap<String, Object>();
			map.put("year", year + "");
			map.put("monthArray", monthArray);
			map.put("incomeArray", incomeArray);
			map.put("payoutArray", payoutArray);
			return map;
		} catch (Exception e) {
			throw new BusinessException(e.getMessage());
		}
		

	}

	public Object getSameMonthSum(int month, List<Map<String, Object>> list) {
		for (Map<String, Object> obj : list) {
			Integer monthM = (Integer) obj.get("month_str");
			Object moneySum = obj.get("moneySum");
			if (month == monthM.intValue()) {
				return moneySum;
			}
		}
		return "0";
	}

	/**
	 * 
	 * 
	 * @param request
	 * @param response
	 * @param dataGrid
	 * @param user
	 */

	@RequestMapping(params = "dgFinanceMonthSum")
	@ResponseBody
	public List<Map<String, Object>> dgFinanceMonthSum(
			HttpServletRequest request, HttpServletResponse response) {
		String sql = "SELECT * from (SELECT MONTH(trade_time) month_str,sum(money) moneySum,'1' flag from flow_detail_income where YEAR(trade_time) = '2015' GROUP BY MONTH(trade_time) union"
				+ "  SELECT MONTH(payout_time) month_str,sum(money) moneySum,'2' flag from flow_detail_payout where YEAR(payout_time) = '2015' GROUP BY MONTH(payout_time)) A ORDER BY month_str";
		List<Map<String, Object>> list = systemService.findForJdbc(sql);
		return list;
	}

}
